package com.csd.dao;

// default package

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.helper.ProjectState;
import com.csd.pojo.EvaluateItem;
import com.csd.pojo.Project;
import com.csd.pojo.Teacher;
import com.csd.vo.EvaluateItemVO;

/**
 * A data access object (DAO) providing persistence and search support for
 * EvaluateItem entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .EvaluateItem
 * @author MyEclipse Persistence Tools
 */

public class EvaluateItemDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(EvaluateItemDAO.class);
	// property constants
	public static final String STAGE = "stage";
	public static final String COMMENT = "comment";
	public static final String EVALUATION = "evaluation";
	public static final String GRADE = "grade";

	protected void initDao() {
		// do nothing
	}

	public void save(EvaluateItem transientInstance) {
		log.debug("saving EvaluateItem instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} 
		catch (RuntimeException re) 
		{
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EvaluateItem persistentInstance) {
		log.debug("deleting EvaluateItem instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	public void update(EvaluateItem evaluateItem){
		this.getHibernateTemplate().saveOrUpdate(evaluateItem);
	}

	public EvaluateItem findById(java.lang.Integer id) {
		log.debug("getting EvaluateItem instance with id: " + id);
		try {
			EvaluateItem instance = (EvaluateItem) getHibernateTemplate().get(
					EvaluateItem.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
  


	
	/**
	 * 根据项目id查出评审项
	 * @param id
	 * @return
	 */
	public List<EvaluateItem> listEvaluateItemByProjectID(Integer id) {
		System.out.println("===================="+id);
		List<EvaluateItem> evaluateItems = null;
		String hql = "from EvaluateItem ei where ei.project.id = ?";
		try {
			evaluateItems = this.getHibernateTemplate().find(hql,id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return evaluateItems;

	}
	
	/**
	 * 查询出该项目的老师评审項
	 * @param projectID 项目id
	 * @param stage 评审阶段
	 * @return 老师评审vo对象列表
	 */
	public List<EvaluateItem> getEvaluateItemOfProject(Integer projectID,String stage){
		List<EvaluateItem> evaluateItems = null;
		Object[]  objects= new Object[]{projectID,stage};
		try {
			String hql = "from EvaluateItem as ei where ei.project.id =? and ei.stage = ?";
			evaluateItems = this.getHibernateTemplate().find(hql,objects);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return evaluateItems;

	}
	
//	/**
//	 * 查看该项目的评审老师
//	 * @param projectID
//	 * @return
//	 */
//	public List<EvaluateItem> getEvaluationTeacher(Integer projectID, String stage)
//	{
//		List<EvaluateItem> evaluateItems = null;
//		String hql = "from EvaluateItem ei where ei.project.id =?";
//		try {
//			evaluateItems = this.getHibernateTemplate().find(hql,projectID);
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//			System.out.println("查看该项目的评审老师抛出异常");
//		}
//
//		return evaluateItems;
//	}
	public List findByExample(EvaluateItem instance) {
		log.debug("finding EvaluateItem instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding EvaluateItem instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from EvaluateItem as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStage(Object stage) {
		return findByProperty(STAGE, stage);
	}

	public List findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

	public List findByEvaluation(Object evaluation) {
		return findByProperty(EVALUATION, evaluation);
	}

	public List findByGrade(Object grade) {
		return findByProperty(GRADE, grade);
	}

	public List findAll() {
		log.debug("finding all EvaluateItem instances");
		try {
			String queryString = "from EvaluateItem";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EvaluateItem merge(EvaluateItem detachedInstance) {
		log.debug("merging EvaluateItem instance");
		try {
			EvaluateItem result = (EvaluateItem) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(EvaluateItem instance) {
		log.debug("attaching dirty EvaluateItem instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	/**通过老师ID获得评审项*/
	public List<EvaluateItem> listEvaluateItemByTeacherID(Integer teacherID) {
		List<EvaluateItem> evaluateList = new ArrayList<EvaluateItem>();
		List<EvaluateItem> evaluateItems = null;
		String hql = "from EvaluateItem ei  where ei.teacher.id = ? and ei.grade = 0 ";
		
		try {
			evaluateItems = this.getHibernateTemplate().find(hql, teacherID);
			for(Iterator<EvaluateItem> iterator = evaluateItems.iterator();iterator.hasNext();){
				 EvaluateItem  eItem= iterator.next();
				 Project project = eItem.getProject();
				if(project.getState().equals(ProjectState.prophase) && eItem.getStage().equals(ProjectState.beforeStage)){
					evaluateList.add(eItem);
				}else if(project.getState().equals(ProjectState.metaphase) && eItem.getStage().equals(ProjectState.middleStage))
				{
					evaluateList.add(eItem);
				}else if(project.getState().equals(ProjectState.finishCheck) && eItem.getStage().equals(ProjectState.afterStage))
				{
					evaluateList.add(eItem);
				}
			}
		} catch (Exception e) {
			System.out.println("EvaluationItemDAO.listEvaluateItemByTeacherID()");
			e.printStackTrace();
		}
		
		return evaluateList;
	}

	public void attachClean(EvaluateItem instance) {
		log.debug("attaching clean EvaluateItem instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	/**更新EvaluateItemDao*/
	public boolean updateEvaluateItem(EvaluateItem evaluateItem) {
		try {
			this.getHibernateTemplate().saveOrUpdate(evaluateItem);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public List<EvaluateItem> listTeacherHistory(Teacher teacher){
		List<EvaluateItem>  evaluateItems = null;
		String queryString = " from EvaluateItem ei where  ei.teacher = ? and ei.grade != 0";
	   	evaluateItems = this.getHibernateTemplate().find(queryString, teacher);
	   	return evaluateItems;
	}
 
	public static EvaluateItemDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (EvaluateItemDAO) ctx.getBean("EvaluateItemDAO");
	}
	
	@SuppressWarnings("unchecked")
	public List<EvaluateItem> getEvaluateItemID(Integer projectID, Integer teacherID)
	{
		List<EvaluateItem> evaluateItemID;
		//String queryString = "from EvaluateItem ei where ei.teacherid = " + teacherID + " and ei.projectid = " + teacherID;
		String queryString = "from EvaluateItem ei where ei.project.id = ? and ei.teacher.id = ?";
		Object[] object = new Object[]{projectID, teacherID};
		
		try
		{
			evaluateItemID = this.getHibernateTemplate().find(queryString, object);
		}
		catch (RuntimeException re)
		{
			throw re;
		}
		
		return evaluateItemID;
		
	}
	
	
}